import sys
import os
import datetime
import pandas as pd
import numpy as np

sys.path.append('..')

from tools.mail import send_report
from tools.setting import DATA_DIR, SENDER, RECEIVER
from tools.tools import csv2xlsx_for_report

ANALYSIS_DATA_DIR = os.path.join(DATA_DIR, 'analysis')
os.makedirs(ANALYSIS_DATA_DIR, exist_ok=True)
REPORT_DIR = os.path.join(ANALYSIS_DATA_DIR, 'report')
os.makedirs(REPORT_DIR, exist_ok=True)


def sending_report(manually=False):
    today = datetime.date.today().strftime('%Y-%m-%d')
    subject = '周报所需数据' + f'_{today}'
    if not manually:
        send_report(subject=subject + '已计算完毕', message='周报数据已计算完毕', sender=SENDER, receiver=RECEIVER)

    # 全市场收益率的统计信息, 之前的文件trading_data_weeks
    file_path = os.path.join(ANALYSIS_DATA_DIR, f'weeks')
    file_name1 = os.path.join(file_path, f'All Coins daily Return Analysis.csv')
    file_name2 = os.path.join(file_path, f'All Coins weekly Return Analysis.csv')
    file_name3 = os.path.join(file_path, f'All Coins monthly Return Analysis.csv')
    file_name4 = os.path.join(file_path, f'All Coins daily Return MA Analysis.csv')
    file_name5 = os.path.join(file_path, f'All Coins weekly Return MA Analysis.csv')
    file_name6 = os.path.join(file_path, f'All Coins monthly Return MA Analysis.csv')

    # 全市场价格回撤信息
    file_path = os.path.join(ANALYSIS_DATA_DIR, f'drawdown')
    file_name7 = os.path.join(file_path, f'drawdown_ratio.csv')

    # 币安杠杆交易费率和aave的usdc杠杆交易费率和defi协议的质押量和借贷量
    file_path = os.path.join(DATA_DIR, f'exchange_information\\margin_interest_rate')
    file_name8 = os.path.join(file_path, f'aave_margin_tvl_borrowed.csv')
    file_name9 = os.path.join(file_path, f'aave_USDC_margin_interest_rate.csv')
    file_name10 = os.path.join(file_path, f'binance_margin_interest_rate.csv')

    # 交易额相对前高回撤信息，分别为所有交易额回撤、九大中心化交易所交易额回撤、BTC交易额回撤信息。
    file_path = os.path.join(ANALYSIS_DATA_DIR, f'drawdown')
    file_name11 = os.path.join(file_path, f'all_amount_analysis.csv')
    file_name12 = os.path.join(file_path, f'all_cex_amount_analysis.csv')
    file_name13 = os.path.join(file_path, f'btc_amount_analysis.csv')

    # btc的收益率分析
    file_name14 = os.path.join(ANALYSIS_DATA_DIR, f'btc_daily_price_analysis.csv')

    # 人民币usdt溢价
    file_path = os.path.join(DATA_DIR, f'usdt_premium')
    file_name15 = os.path.join(file_path, f'usdtcny_premium_btc126.csv')
    file_name16 = os.path.join(file_path, f'usdtcny_premium_okex.csv')

    # glassnode部分指标的全历史百分位，和统计分位点数值
    file_path = os.path.join(ANALYSIS_DATA_DIR, f'glassnode')
    file_name17 = os.path.join(file_path, f'valuation_describe_percentile_analysis_start_2017-01-01.csv')
    file_name18 = os.path.join(file_path, f'valuation_whole_percentile_analysis_start_2017-01-01.csv')

    # binance借贷利率 无法更新 coinglass过期
    # file_path = os.path.join(DATA_DIR, f'borrow_interest')
    # file_name19 = os.path.join(file_path, f'Binance_USDT_borrow_interest_data.xlsx')

    # 中心化交易所成交额占比
    file_path = os.path.join(ANALYSIS_DATA_DIR, f'cex')
    file_name20 = os.path.join(file_path, f'all_cex_spot_amount.csv')
    file_name21 = os.path.join(file_path, f'all_cex_deri_amount.csv')

    # ETH现货交易额分析
    file_path = os.path.join(ANALYSIS_DATA_DIR, f'drawdown')
    file_name22 = os.path.join(file_path, f'eth_amount_analysis.csv')

    files = [file_name1, file_name2, file_name3, file_name4, file_name5, file_name6, file_name7,file_name8, file_name9,
             file_name10, file_name11, file_name12, file_name13, file_name14, file_name15, file_name16, file_name17,
             file_name18, file_name20, file_name21, file_name22]

    price = pd.read_csv('E:\\data\\ohlcvm_data\\kline\\BTC_24h.csv', index_col='end_date')
    # price['log_prices'] = np.log(price['close'])
    price['log_price'] = np.log10(price['close'])
    report_files = csv2xlsx_for_report(files, REPORT_DIR, price[['log_price']])

    send_report(subject=subject, message='周报-1', sender=SENDER, receiver=RECEIVER, files=report_files[0])
    send_report(subject=subject, message='周报-2', sender=SENDER, receiver=RECEIVER, files=report_files[1:3])
    send_report(subject=subject, message='周报-3', sender=SENDER, receiver=RECEIVER, files=report_files[3:])

if __name__ == '__main__':
    # sending_report(manually=True)
    sending_report()
